<?php
class DailyUtility
{
    public static function analyze($date)
    {
        DailyModel::analyze($date);
    }
    
    public static function delete($date)
    {
        DailyModel::deleteByDate($date);
    }
    
    public static function sendMail($date)
    {
        // params
        $params = array();
        $params['daily'] = DailyModel::getByDate($date);
        $params['daily_30'] = DailyModel::getByDate(DateUtility::ago($date, 30));
        $params['gifts'] = LogModel::getGiftAmount($date);
        $params['stickers'] = LogModel::getStickerAmount($date);
        if($params['daily']['a1_total'] == 0) // check error
        {
            return;
        }

        // send
        $subject = 'OakClub Daily Report '.$date;
        $view = 'daily';
        $emails = ReportModel::getDailyEmails();
        foreach($emails as $email)
        {
            for($i = 0; $i < 3; $i++)
            {
                try
                {
                    $isSuccess = (int) MailUtility::send($email, $subject, $view, $params);
                    echo "\n".date('Y-m-d H:i:s').'|'.$email.'|'.$isSuccess."\n";

                    if($isSuccess == 1)
                    {
                        break;
                    }
                }
                catch(Exception $e)
                {
                    echo "\n".$e->getMessage()."\n";
                }
                sleep(2);
            }
        }
    }
    
    public static function getDashboard()
    {
        $data = array();
        
        // get daily
        $toDate = DateUtility::yesterday();
        $fromDate = DateUtility::ago(DateUtility::today(), CONST_DASHBOARD_PERIOD);
        $rows = DailyModel::getByPeriod($fromDate, $toDate);
        
        // data
        foreach($rows as $row)
        {
            $date = DateUtility::convertFormat($row['date'], 'Y-m-d', 'd-m');
            
            $data['new_user'][$date] = (int) $row['new_user'];
            $data['new_male'][$date] = (int) $row['new_male'];
            $data['new_female'][$date] = (int) $row['new_female'];
            $data['a1'][$date] = (int) $row['a1_total'];
            $data['a7'][$date] = (int) $row['a7_total'];
            $data['a30'][$date] = (int) $row['a30_total'];
            $data['snapshot_total'][$date] = (int) $row['snapshot_total'];
            $data['snapshot_like'][$date] = (int) $row['snapshot_like'];
            $data['snapshot_dislike'][$date] = (int) $row['snapshot_dislike'];
            $data['mutual_match'][$date] = (int) $row['snapshot_mutual_match'];
            $data['search'][$date] = (int) $row['search'];
            $data['send_gift'][$date] = (int) $row['send_gift'];
            $data['chat'][$date] = (int) $row['chat'];
            $data['invite'][$date] = (int) $row['invite'];
            $data['view_profile'][$date] = (int) $row['view_profile'];
        }
        
        return $data;
    }

    public static function getAll()
    {
        return DailyModel::getAll();
    }

    public static function getCSV()
    {
        $data = self::getAll();
        foreach($data as &$daily)
        {
            // total user per country
            $totalUserPerCountry = '';
            if(is_array($daily['total_user_per_country']))
            {
                foreach($daily['total_user_per_country'] as $country)
                {
                    $totalUserPerCountry .= $country['name'].'('.$country['code'].')'.':'.$country['amount'].';';
                }
            }
            $daily['total_user_per_country'] = @substr($totalUserPerCountry, 0, -1);

            // new user per country
            $newUserPerCountry = '';
            if(is_array($daily['new_user_per_country']))
            {
                foreach($daily['new_user_per_country'] as $country)
                {
                    $newUserPerCountry .= $country['name'].'('.$country['code'].')'.':'.$country['amount'].';';
                }
            }
            $daily['new_user_per_country'] = @substr($newUserPerCountry, 0, -1);
        }
        unset($daily);

        $csv = FileUtility::convertFromArrayToCSV($data);
        return $csv;
    }
}
